package com.ruoyi.userManage.controller;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.blindBoxManage.mapper.BlindBoxMapper;
import com.ruoyi.blindBoxManage.mapper.BoxMapper;
import com.ruoyi.blindBoxManage.mapper.ClassMapper;
import com.ruoyi.blindBoxManage.mapper.PlayTypeMapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.userManage.domain.Package;
import com.ruoyi.userManage.domain.User;
import com.ruoyi.userManage.mapper.UserMapper;
import com.ruoyi.userManage.service.IPackageService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 背包记录Controller
 *
 * @author ruoyi
 * @date 2025-08-12
 */
@RestController
@RequestMapping("/package/package")
public class PackageController extends BaseController {
    @Autowired
    private IPackageService packageService;
    @Resource
    private ClassMapper classMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private BlindBoxMapper blindBoxMapper;
    @Resource
    private BoxMapper boxMapper;
    @Resource
    private PlayTypeMapper playTypeMapper;

    /**
     * 查询背包记录列表
     */
    @PreAuthorize("@ss.hasPermi('package:package:list')")
    @GetMapping("/list")
    public TableDataInfo list(Package packages) {
        if (!StrUtil.isEmpty(packages.getClassName())) {
            Integer classId = classMapper.selectIdByName(packages.getClassName());
            if (classId != null) {
                packages.setClassId(classId);
            } else {
                return getDataTable(Collections.emptyList());
            }
        }
        if (!StrUtil.isEmpty(packages.getBlindBoxName())) {
            Long blindId = blindBoxMapper.selectIdByName(packages.getBlindBoxName());
            if (blindId != null) {
                packages.setBlindBoxId(blindId);
            } else {
                return getDataTable(Collections.emptyList());
            }
        }
        if (!StrUtil.isEmpty(packages.getBoxName())) {
            Long boxId = boxMapper.selectIdByName(packages.getBoxName());
            if (boxId != null) {
                packages.setBoxId(boxId);
            } else {
                return getDataTable(Collections.emptyList());
            }
        }
        if (!StrUtil.isEmpty(packages.getUserPhone())) {
            Long userId = userMapper.selectIdByPhone(packages.getUserPhone());
            if (userId != null) {
                packages.setUserId(userId);
            } else {
                return getDataTable(Collections.emptyList());
            }
        }
        if (!StrUtil.isEmpty(packages.getPlayTypeName())) {
            Integer playTypeId = playTypeMapper.selectIdByName(packages.getPlayTypeName());
            if (playTypeId != null) {
                packages.setPlayTypeId(playTypeId);
            } else {
                return getDataTable(Collections.emptyList());
            }
        }
        if (packages.getSixUserId() != null ) {
            Long userId = userMapper.selectUserIdBySixUserId(packages.getSixUserId());
            if (userId != null) {
                packages.setUserId(userId);
            } else {
                return getDataTable(Collections.emptyList());
            }
        }
        startPage();
        List<Package> list = packageService.selectPackageList(packages);
        if (!CollectionUtils.isEmpty(list)) {
            for (Package record : list) {
                String blindBoxName = blindBoxMapper.selectNameById(record.getBlindBoxId());
                record.setBlindBoxName(!StrUtil.isEmpty(blindBoxName) ? blindBoxName : "");
                String boxName = boxMapper.selectNameById(record.getBoxId());
                record.setBoxName(!StrUtil.isEmpty(boxName) ? boxName : "");
                String className = classMapper.selectNameById(record.getClassId());
                record.setClassName(!StrUtil.isEmpty(className) ? className : "");
                String phone = userMapper.selectPhoneById(record.getUserId());
                record.setUserPhone(!StrUtil.isEmpty(phone) ? phone : "");
                String playTypeName = playTypeMapper.selectNameById(record.getPlayTypeId());
                record.setPlayTypeName(!StrUtil.isEmpty(playTypeName) ? playTypeName : "");
                User user = userMapper.selectUserById(record.getUserId());
                record.setUserId(!ObjectUtils.isEmpty(user) ? user.getUserId() : 0);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出背包记录列表
     */
    @PreAuthorize("@ss.hasPermi('package:package:export')")
    @Log(title = "背包记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Package packages) {
        if (!StrUtil.isEmpty(packages.getClassName())) {
            Integer classId = classMapper.selectIdByName(packages.getClassName());
            if (classId != null) {
                packages.setClassId(classId);
            }
        }
        if (!StrUtil.isEmpty(packages.getBlindBoxName())) {
            Long blindId = blindBoxMapper.selectIdByName(packages.getBlindBoxName());
            if (blindId != null) {
                packages.setBlindBoxId(blindId);
            }
        }
        if (!StrUtil.isEmpty(packages.getBoxName())) {
            Long boxId = boxMapper.selectIdByName(packages.getBoxName());
            if (boxId != null) {
                packages.setBoxId(boxId);
            }
        }
        if (!StrUtil.isEmpty(packages.getUserPhone())) {
            Long userId = userMapper.selectIdByPhone(packages.getUserPhone());
            if (userId != null) {
                packages.setUserId(userId);
            }
        }
        if (!StrUtil.isEmpty(packages.getPlayTypeName())) {
            Integer playTypeId = playTypeMapper.selectIdByName(packages.getPlayTypeName());
            if (playTypeId != null) {
                packages.setPlayTypeId(playTypeId);
            }
        }
        if (packages.getSixUserId() != null ) {
            Long userId = userMapper.selectUserIdBySixUserId(packages.getSixUserId());
            if (userId != null) {
                packages.setUserId(userId);
            }
        }
        List<Package> list = packageService.selectPackageList(packages);
        if (!CollectionUtils.isEmpty(list)) {
            for (Package record : list) {
                String blindBoxName = blindBoxMapper.selectNameById(record.getBlindBoxId());
                record.setBlindBoxName(!StrUtil.isEmpty(blindBoxName) ? blindBoxName : "");
                String boxName = boxMapper.selectNameById(record.getBoxId());
                record.setBoxName(!StrUtil.isEmpty(boxName) ? boxName : "");
                String className = classMapper.selectNameById(record.getClassId());
                record.setClassName(!StrUtil.isEmpty(className) ? className : "");
                String phone = userMapper.selectPhoneById(record.getUserId());
                record.setUserPhone(!StrUtil.isEmpty(phone) ? phone : "");
                String playTypeName = playTypeMapper.selectNameById(record.getPlayTypeId());
                record.setPlayTypeName(!StrUtil.isEmpty(playTypeName) ? playTypeName : "");
            }
        }
        ExcelUtil<Package> util = new ExcelUtil<Package>(Package.class);
        util.exportExcel(response, list, "背包记录数据");
    }

    /**
     * 获取背包记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('package:package:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(packageService.selectPackageById(id));
    }

    /**
     * 新增背包记录
     */
    @PreAuthorize("@ss.hasPermi('package:package:add')")
    @Log(title = "背包记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Package packages) {
        return toAjax(packageService.insertPackage(packages));
    }

    /**
     * 修改背包记录
     */
    @PreAuthorize("@ss.hasPermi('package:package:edit')")
    @Log(title = "背包记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Package packages) {
        return toAjax(packageService.updatePackage(packages));
    }

    /**
     * 删除背包记录
     */
    @PreAuthorize("@ss.hasPermi('package:package:remove')")
    @Log(title = "背包记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(packageService.deletePackageByIds(ids));
    }
}
